﻿namespace Sidvall.CodeGenerator
{
    public class DomainObjectIndex
    {
        #region Business Members

        public string Name { get; set; }
        public string NameInDataStore { get; set; }
        public bool? IsUnique { get; set; }
        public DomainObjectPropertyCollection Fields { get; private set; }

        #region GetKey

        public static string GetKey(DomainObjectIndex item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");

            return item.Name;
        }

        #endregion
        #region Import

        public void Import(DomainObjectIndex item)
        {
            if (item == null)
                return;

            if (item.NameInDataStore != null)
                this.NameInDataStore = item.NameInDataStore;
            if (item.IsUnique != null)
                this.IsUnique = item.IsUnique;
        }

        #endregion
        #region IsUniqueResolved

        public bool IsUniqueResolved
        {
            get
            {
                if (this.IsUnique != null)
                    return this.IsUnique.Value;
                return false;
            }
        }

        #endregion
        #region NameResolved

        public string NameResolved
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(this.Name))
                    return this.Name;
                var sb = new System.Text.StringBuilder();
                foreach (var item in this.Fields)
                    sb.Append(item.Name);
                return sb.ToString();
            }
        }

        #endregion

        #endregion
        #region Constructors

        public DomainObjectIndex()
        {
            this.Fields = new DomainObjectPropertyCollection();
        }

        #endregion
    }
}
